import {Injectable, NestInterceptor, ExecutionContext, CallHandler} from '@nestjs/common';
import {Observable} from 'rxjs';
import {tap, map} from 'rxjs/operators';

/**
 * 返回拦截器
 */

@Injectable()
export class ResInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {

        const now = Date.now();
        return next
            .handle()
            .pipe(
                map(data => {
                    return {
                        data,
                        code: 0,
                        message: '请求成功',
                        time: (new Date()).toLocaleString(),
                    };
                })
            );
    }
}
